package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.PayOrderContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.order.NeedPay;
import com.ruitu.router_module.bean.order.PayReq;

public class PayOrderPresenter extends PayOrderContract.Presenter {
    @Override
    public void aliPayForApp(String status, String orderId) {
        mModel.aliPayForApp(status, orderId)
                .subscribe(new SubscriberImpl<BaseResult<String>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("获取支付信息失败");
                    }

                    @Override
                    public void onNext(BaseResult<String> stringBaseResult) {
                        mView.hideProgress();
                        if (stringBaseResult.isSuccess()) {
                            mView.setPaySuc(stringBaseResult.getData());
                        } else {
                            ToastUtils.showShort(stringBaseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void balanceForApp(String accountId, String code, String orderId) {
        mModel.balanceForApp(accountId, code, orderId)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("支付失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setPaySuc();
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void weixinPayForApp(String status, String orderId) {
        mModel.weixinPayForApp(status, orderId)
                .subscribe(new SubscriberImpl<BaseResult<PayReq>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("获取支付失败");
                    }

                    @Override
                    public void onNext(BaseResult<PayReq> payReqBaseResult) {
                        mView.hideProgress();
                        if (payReqBaseResult.isSuccess()) {
                            mView.setWeixinPay(payReqBaseResult.getData());
                        } else {
                            ToastUtils.showShort(payReqBaseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void getOrderMoney(String orderId) {
        mModel.getOrderMoney(orderId)
                .subscribe(new SubscriberImpl<BaseResult<NeedPay>>() {
                    @Override
                    public void onError(Throwable e) {
                        Logg.i(e.toString());
                        mView.hideProgress();
                    }

                    @Override
                    public void onNext(BaseResult<NeedPay> baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setNeedPay(baseResult.getData());
                        }
                    }
                });
    }
}
